package com.feng.seata.service;

import com.feng.common.model.Result;
import com.feng.seata.mapper.OrderMapper;
import com.feng.seata.model.OrderModel;
import com.feng.seata.rpc.ProductClient;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

@Service
public class OrderService {

    @Resource
    private ProductClient productClient;

    @Resource
    private OrderMapper orderMapper;

//    @Transactional
    public void seataDemo(Integer buyNum, Boolean hasError) {
        //下单操作
        OrderModel orderModel = new OrderModel();
        orderModel.setOrderName("测试数据");
        orderModel.setBuyNum(buyNum);
        orderModel.setProductId(1L);
        orderMapper.insert(orderModel);

        //减库存（这里参数什么的就自己脑补了）
        Result<?> result = productClient.minusStock(buyNum);

        //异常模拟
        if (hasError) {
            int i = 1 / 0;
        }
    }
}
